package www.com.library.app;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Field;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import www.com.library.dialog.ToastPopWindow;

/**
 * 公共基础fragment
 *
 * @author Reeta.zou
 */
public abstract class PushMsgTabFragment extends Fragment {
    private CompositeDisposable mCompositeDisposable; //RxBus的注册事件
    private PushMsgTabFragment mParent;
    public FragmentManager mFragmentManager = null;
    public FragmentTransaction mFragmentTransaction = null;
    /**
     * 气泡提示
     */
    protected ToastPopWindow mToastPop;

    protected View mRootView;//根布局

    public PushMsgTabFragment() {
        super();
        mParent = null;
    }

    public PushMsgTabFragment getParent() {
        return mParent;
    }

    public void setParent(PushMsgTabFragment parent) {
        mParent = parent;
    }

    public boolean postToUIThread(Runnable runnable) {
        getActivity().runOnUiThread(runnable);
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.e(this.getClass().getSimpleName() + " onCreate");
        Fragment parent = getParentFragment();
        mFragmentManager = getChildFragmentManager();

        if (parent instanceof PushMsgTabFragment)
            mParent = (PushMsgTabFragment) parent;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        Logger.e(this.getClass().getSimpleName() + " onCreateView");
        mRootView = inflater.inflate(getLayoutId(), null);
        initLayoutView();
        registerRxBus();
        initViewData();
        return mRootView;
    }

    /**
     * RxBus注册事件管理类
     *
     * @param subscription
     */
    public void bindSubscription(Disposable subscription) {
        if (this.mCompositeDisposable == null) {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        this.mCompositeDisposable.add(subscription);
    }

    /**
     * 气泡提示
     *
     * @param errorMessage
     */
    public void showToastPopWindow(String errorMessage) {
        if (getActivity() != null) {
            new ToastPopWindow(getActivity(), errorMessage).show();
        }

    }

    /**
     * 注册RxBus相关联的监听事件
     */
    public void registerRxBus() {
    }

    /**
     * @return 返回当前界面加载的布局id
     */
    protected abstract int getLayoutId();

    /**
     * 初始化当前界面的控件
     */
    protected abstract void initLayoutView();

    /**
     * 初始化界面控件的值和条件
     */
    protected abstract void initViewData();


    public void onAccountNotify() {
    }

    public void onSymbolNotify() {
    }

    public void onSymbolNotify(int codeId) {
    }

    public void onOrderSuccessNotify(int notifyId, int orderId) {
    }

    public void onOrderFailNotify(int notifyId, int resultCode) {
    }

    public void onSendQuote() {
        //主面板根据每个版本情况发送订阅信息
    }

    /**
     * 行情服务器监听调用的通知方法
     *
     * @param isConntion
     */
    public void quoteServerNotice(Boolean isConntion) {
    }

    /**
     * 交易服务器监听调用的通知方法
     *
     * @param isConntion
     */
    public void tradeServerNotice(Boolean isConntion) {

    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (null != mToastPop) {
            mToastPop.hidden();
        }
        Logger.e(this.getClass().getSimpleName() + " onDetach");
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Logger.e(this.getClass().getSimpleName() + " onSaveInstanceState");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.e(this.getClass().getSimpleName() + " onDestroy");
        if (this.mCompositeDisposable != null) {
            this.mCompositeDisposable.clear();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        Logger.e(this.getClass().getSimpleName() + " onPause");
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        Logger.e(this.getClass().getSimpleName() + " onResume");
    }

    @Override
    public void onStart() {
        super.onStart();
        Logger.e(this.getClass().getSimpleName() + " onStart");
    }

    @Override
    public void onStop() {
        super.onStop();
        Logger.e(this.getClass().getSimpleName() + " onStop");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Logger.e(this.getClass().getSimpleName() + " onHiddenChanged " + hidden);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Logger.e(this.getClass().getSimpleName() + " onDestroyView ");
    }

    public void onRefresh() {
    }

    //不得已才加的代码，目的是为了改善多tab切换的卡顿情况; 如果没有发生ViewPager切换卡顿的情况，可不看这一部分代码
    protected String mCurTypeId = "";//类型id
    protected String mCurTypeName = "";//产品类型名字

    public boolean isFragmentReady() {
        return isFragmentReady;
    }

    protected boolean isFragmentReady = false;

    protected CountDownTimer mCountDownTimer;

    protected void startCountDownTimer() {
        if (mCountDownTimer != null)
            mCountDownTimer.cancel();
        mCountDownTimer = new CountDownTimer(1500, 100) {//总计1500ms的倒计时，每次轮询间隔100ms
            @Override
            public void onTick(long millisUntilFinished) {
                isFragmentReady = false;
            }

            @Override
            public void onFinish() {
                isFragmentReady = true;
                onSendQuote();//计时完成之后再进行行情报价注册
            }
        };
        mCountDownTimer.start();
    }
}
